package com.yht.downloadimage.webdriver;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

import com.yht.downloadimage.util.StaticClazz;


public class GetWebDriver {
	//火狐浏览器
	public synchronized static WebDriver getFireFoxDriver(String proxyIpAndPort){
		System.setProperty("webdriver.firefox.bin", StaticClazz.FIREFOXDRIVERPATH);
		ProfilesIni allProfiles = new ProfilesIni(); 
		FirefoxProfile profile = allProfiles.getProfile("default");
		DesiredCapabilities caps = DesiredCapabilities.firefox();
		caps.setJavascriptEnabled(true);
		caps.setCapability("firefox.page.settings.userAgent","Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:41.0) Gecko/20100101 Firefox/41.0");
		caps.setCapability("firefox_profile", profile);
		if(proxyIpAndPort!=null&&proxyIpAndPort.trim().length()>3){
			caps.setCapability(CapabilityType.PROXY, new Proxy().setHttpProxy(proxyIpAndPort).setFtpProxy(proxyIpAndPort));
		}
		
		WebDriver webDriver=new FirefoxDriver(caps);
		webDriver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);
		webDriver.manage().window().maximize();
		return webDriver;
	}
	
	//谷歌浏览器
	public synchronized static WebDriver getChromeDriver(String proxyIpAndPort){
		System.setProperty("webdriver.chrome.driver",StaticClazz.CHROMEDRIVERPATH);
		ChromeOptions options = new ChromeOptions();
//		options.addArguments("user-data-dir=C:/Users/user_name/AppData/Local/Google/Chrome/User Data");
		options.addArguments("--test-type", "--ignore-certificate-errors");
		options.addArguments("--test-type", "--start-maximized","no-default-browser-check"); 
		options.addArguments("--proxy-server=socks5://" + proxyIpAndPort.split(":")[0] + ":" + proxyIpAndPort.split(":")[1]);
		DesiredCapabilities caps = DesiredCapabilities.chrome();
		if(proxyIpAndPort!=null&&proxyIpAndPort.trim().length()>3){
			caps.setCapability(CapabilityType.PROXY, new Proxy().setHttpProxy(proxyIpAndPort).setFtpProxy(proxyIpAndPort));
		}
		caps.setCapability(ChromeOptions.CAPABILITY, options);
		WebDriver webDriver=new ChromeDriver(options);
		webDriver.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS);
		webDriver.manage().window().maximize();
		return webDriver;
	}
	
	
	//PhantomJS
	public synchronized static WebDriver getPhantomJSDriver(String proxyIpAndPort){
		System.setProperty("phantomjs.binary.path", StaticClazz.PHANTOMJSDRIVERPATH);
		DesiredCapabilities caps = DesiredCapabilities.phantomjs();
		caps.setCapability("phantomjs.page.settings.userAgent",
				"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36"+ Math.random()*10000);
		String[] strs={"--load-images=true","--webdriver-loglevel=NONE" };
		caps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, strs);
		caps.setCapability("takesScreenshot", false);
		caps.setJavascriptEnabled(true);
		if(proxyIpAndPort!=null&&proxyIpAndPort.trim().length()>3){
			caps.setCapability(CapabilityType.PROXY, new Proxy().setHttpProxy(proxyIpAndPort).setFtpProxy(proxyIpAndPort));
		}
		WebDriver webDriver = new PhantomJSDriver(caps);
		webDriver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
		return webDriver;
	}
	

}
